package com.dwxt.race.controller;

import com.dwxt.common.annotation.ValidateFiled;
import com.dwxt.common.annotation.ValidateGroup;
import com.dwxt.common.base.BaseResult;
import com.dwxt.race.entity.Race;
import com.dwxt.race.service.RaceService;
import com.dwxt.race.service.feign.FileService;
import org.apache.commons.lang.StringUtils;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.PostMapping;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RequestParam;
import org.springframework.web.bind.annotation.RestController;
import org.springframework.web.multipart.MultipartFile;

import java.io.IOException;
import java.util.Date;

@RequestMapping("/race/raceList")
@RestController
public class RaceController {

    @Autowired
    private RaceService raceService;
    @Autowired
    private FileService fileService;


    @RequestMapping("/insertOrUpdateRace")
    @ValidateGroup(fileds = {
            @ValidateFiled(index = 0, filedName = "raceName", notNull = true),
            @ValidateFiled(index = 0, filedName = "registrationStartTime", notNull = true),
            @ValidateFiled(index = 0, filedName = "registrationEndTime", notNull = true),
            @ValidateFiled(index = 0, filedName = "raceStartTime", notNull = true),
            @ValidateFiled(index = 0, filedName = "raceEndTime", notNull = true),
            @ValidateFiled(index = 0, filedName = "conditions", notNull = true),
            @ValidateFiled(index = 0, filedName = "cost", notNull = true,minVal = 0),
            @ValidateFiled(index = 0, filedName = "organizer", notNull = true),
            @ValidateFiled(index = 0, filedName = "address", notNull = true),
            @ValidateFiled(index = 0, filedName = "rule", notNull = true),
            @ValidateFiled(index = 0, filedName = "integral", notNull = true),
            @ValidateFiled(index = 0, filedName = "commitment", notNull = true),
            @ValidateFiled(index = 0, filedName = "imgurl", notNull = true),
            @ValidateFiled(index = 0, filedName = "maxPerson", notNull = true)})
    public BaseResult insertOrUpdateRace(Race race){
        if (race.getId()!=null){
            return raceService.updateRace(race);
        }else {
            return raceService.createRace(race);
        }
    }

    /**
     * 赛事禁用/启用接口
     * @param id
     * @return
     */
    @RequestMapping("/delete")
    public BaseResult deleteRaceById(Integer id){
        return raceService.deleteRace(id);
    }

    @RequestMapping("/selectRaceList")
    public BaseResult selectRaceList(@RequestParam(required = true) Integer pageNum,
                                     @RequestParam(required = true)Integer pageSize,
                                     @RequestParam(required = false)String raceName,
                                     @RequestParam(required = false) Integer status
                                     ){
        if (StringUtils.isNotEmpty(raceName) || status!=null){
           return raceService.selectRaceListByQuery(pageNum,pageSize,raceName,status);
        }
        else {
           return raceService.selectRaceList(pageNum,pageSize,-1);
        }
    }

    @PostMapping("/uploadFile")
    public BaseResult uploadFile(MultipartFile file) throws IOException {
       return fileService.saveFileLocal(file);
    }


}
